﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Country_Entity : EntityBase<COUNTRY>
    {
        public override RESP_Info Add(COUNTRY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into COUNTRY(COUNTRY_ID,COUNTRY_NAME,BELONG) values (@COUNTRY_ID,@COUNTRY_NAME,@BELONG)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("COUNTRY_ID", model.COUNTRY_ID));
                    cmd.Parameters.Add(new SqlParameter("COUNTRY_NAME", model.COUNTRY_NAME));
                    cmd.Parameters.Add(new SqlParameter("BELONG", model.BELONG));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加国家成功</p></h5>";
                    info.Url = "CountryList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加国家失败</p></h5>";
                    info.Url = "CountryAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加国家失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(COUNTRY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update COUNTRY set BELONG=@BELONG, COUNTRY_NAME=@COUNTRY_NAME where COUNTRY_ID=@COUNTRY_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("COUNTRY_ID", model.COUNTRY_ID));
                    cmd.Parameters.Add(new SqlParameter("COUNTRY_NAME", model.COUNTRY_NAME));
                    cmd.Parameters.Add(new SqlParameter("BELONG", model.BELONG));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑国家成功</p></h5>";
                    info.Url = "CountryList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑国家失败</p></h5>";
                    info.Url = "CountryAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑国家失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(COUNTRY country)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from COUNTRY where COUNTRY_ID=@COUNTRY_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("COUNTRY_ID", country.COUNTRY_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除国家成功</p></h5>";
                    info.Url = "CountryList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除国家失败</p></h5>";
                    info.Url = "CountryList.aspx";

                    Tom.Net.Log.LogManager.Error("删除国家失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override COUNTRY GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select COUNTRY_ID,COUNTRY_NAME,BELONG from COUNTRY where COUNTRY_ID=@COUNTRY_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("COUNTRY_NAME", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        COUNTRY country = new COUNTRY();
                        if (!reader.IsDBNull(0)) country.COUNTRY_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) country.COUNTRY_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) country.BELONG = reader.GetString(2);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return country;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取国家数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override COUNTRY GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        COUNTRY country = new COUNTRY();
                        if (!reader.IsDBNull(0)) country.COUNTRY_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) country.COUNTRY_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) country.BELONG = reader.GetString(2);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return country;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取国家数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<COUNTRY> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<COUNTRY> countrys = new List<COUNTRY>();
                    while (reader.Read())
                    {
                        COUNTRY country = new COUNTRY();
                        if (!reader.IsDBNull(0)) country.COUNTRY_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) country.COUNTRY_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) country.BELONG = reader.GetString(2);

                        countrys.Add(country);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return countrys;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取国家数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
